package com.huatai.exchange.integration.base.impl;

import com.alibaba.fastjson.JSON;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.integration.http.HttpClientUtils;
import com.google.common.collect.Maps;
import com.huatai.base.facade.bean.user.QueryUserVoListResp;
import com.huatai.base.facade.bean.user.QueryUserVoResp;
import com.huatai.base.facade.bean.user.UserVo;
import com.huatai.exchange.integration.base.AbstractBaseService;
import com.huatai.exchange.integration.base.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends AbstractBaseService implements UserService {

	private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

	@Override
	public List<UserVo> queryUserList(UserVo user) throws BusinessException {
		String url = baseUrl + "user";
		Map<String, Object> paraMap = Maps.newHashMap();
		if (StringUtils.isNotBlank(user.getDepartmentId())) {
			paraMap.put("departmentId", user.getDepartmentId());
		}
		if (StringUtils.isNotBlank(user.getUserId())) {
			paraMap.put("userId", user.getUserId());
		}
		if (StringUtils.isNotBlank(user.getUserName())) {
			paraMap.put("userName", user.getUserName());
		}
		if (StringUtils.isNotBlank(user.getName())) {
			paraMap.put("name", user.getName());
		}
		String result = HttpClientUtils.query(url, paraMap);
		QueryUserVoListResp resp = JSON.parseObject(result, QueryUserVoListResp.class);
		return resp.getUserVos();
	}

	@Override
	public List<UserVo> queryAllUserList(UserVo user) throws BusinessException {
		String url = baseUrl + "user/all";
		Map<String, Object> paraMap = Maps.newHashMap();
		if (StringUtils.isNotBlank(user.getDepartmentId())) {
			paraMap.put("departmentId", user.getDepartmentId());
		}
		if (StringUtils.isNotBlank(user.getUserId())) {
			paraMap.put("userId", user.getUserId());
		}
		if (StringUtils.isNotBlank(user.getUserName())) {
			paraMap.put("userName", user.getUserName());
		}
		if (StringUtils.isNotBlank(user.getName())) {
			paraMap.put("name", user.getName());
		}
		String result = HttpClientUtils.query(url, paraMap);
		QueryUserVoListResp resp = JSON.parseObject(result, QueryUserVoListResp.class);
		return resp.getUserVos();
	}

	@Override
	public Map<String, String> queryUserMap(UserVo userVo) throws BusinessException {
		Map<String, String> map = Maps.newHashMap();
		List<UserVo> list = queryUserList(userVo);
		for (UserVo u : list) {
			map.put(u.getUserId(), u.getName() + "  " + u.getDepartmentName());
		}
		return map;
	}

	@Override
	public List<UserVo> queryUserListByDepartmentId(String departmentId) throws BusinessException {
		UserVo user = new UserVo();
		user.setDepartmentId(departmentId);
		return queryUserList(user);
	}

	@Override
	public UserVo queryById(String userId) throws BusinessException {
		String url = baseUrl + "user/" + userId;
		String result = HttpClientUtils.query(url, null);
		QueryUserVoResp resp = JSON.parseObject(result, QueryUserVoResp.class);
		return resp.getUserVo();
	}

	@Override
	public void doLogin(String userName, String password) throws BusinessException {
		String url = baseUrl + "user/dologin";
		Map<String, Object> paramMap = Maps.newHashMap();
		paramMap.put("userName", userName);
		paramMap.put("password", password);
		HttpClientUtils.post(url, paramMap);
	}

	@Override
	public UserVo queryByUserName(String userName) throws BusinessException {
		UserVo user = new UserVo();
		user.setUserName(userName);
		List<UserVo> users = queryUserList(user);
		if (ListUtils.isEmpty(users))
			return null;
		return users.get(0);
	}

	@Override
	public void changePwd(String userId, String oldPwd, String newPwd) throws BusinessException {
		String url = baseUrl + "user/changepwd";
		Map<String, Object> paramMap = Maps.newHashMap();
		paramMap.put("userId", userId);
		paramMap.put("oldPwd", oldPwd);
		paramMap.put("newPwd", newPwd);
		HttpClientUtils.post(url, paramMap);
	}

	/**
	 * 更新用户状态
	 */
	@Override
	public void changeUserStatus(String userId, String oldStatus, String newStatus) throws BusinessException {
		String url = baseUrl + "user/changeuserstatus";
		Map<String, Object> paramMap = Maps.newHashMap();
		paramMap.put("userId", userId);
		paramMap.put("oldStatus", oldStatus);
		paramMap.put("newStatus", newStatus);

		HttpClientUtils.post(url, paramMap);
	}

	/**
	 * 重设用户密码
	 */
	@Override
	public void resetUserPassWord(String userId) throws BusinessException {
		String url = baseUrl + "user/resetuserpassword";
		Map<String, Object> paramMap = Maps.newHashMap();
		paramMap.put("userId", userId);

		HttpClientUtils.post(url, paramMap);
	}

}
